##################################################################################################################################
#
# Copyright (C) 2008-2013 Mind System Connection. Lda. Part of Daidze Software, created by Siqsuruq. All other rights reserved.
#
# Mind System Connection Webpage: www.msysc.org
# Daidze Software Page: www.daidze.com www.daidze.org
# E-mail: admin@msysc.org, siqsuruq@gmail.com
#
##################################################################################################################################

# Evaluates Namespaces by name of module folder
# proc create_namespaces {} {
	# set folders [glob -nocomplain -directory $::mod_dir *]
	set folders [glob -nocomplain -directory [file join res modules] *]
	foreach f $folders {

		namespace eval [file tail $f] {

			if {[catch {set files [glob -directory [file join $::f] *.tcl]} errmsg]} {
			} else {
				set files [glob -directory [file join $f] *.tcl]
				foreach file $files {
				# puts $file
					source $file
				}
			}
		}
	}
# }

proc read_config_db {db} {
	set conf_db [my_sqlitedb new $db.sdb]
	set sections [$conf_db show_tables]
	foreach section $sections {
		set result [$conf_db execute_query "SELECT * FROM $section" dict]
		foreach line [dict values $result] {
			foreach key [dict keys [dict values $line]] val [dict values [dict values $line]] {
				set ::$key $val
			}
		}
	}
}

proc write_config_db {db config_dict} {
	set conf_db [my_sqlitedb new [file join conf $db.sdb]]
	set sections [$conf_db show_tables]
	foreach section_to_write [dict keys $config_dict] {
		set section_values [dict get $config_dict $section_to_write]
		foreach col [dict keys $section_values] val [dict values $section_values] {
			$conf_db update_all $section_to_write [dict create daidze_var $col daidze_var_val $val]
		}
	}
	read_config_db [file join conf $db]
}



proc write_config_external_progs {xml_obj} {
	set b [dict create]
	dict set b external_programs [$xml_obj get_values]
	write_config_db daidze $b
	$xml_obj load_main config config.xml $::main 1
}

proc write_config_language {xml_obj} {
	set b [dict create]
	dict set b language [$xml_obj get_values]
	write_config_db daidze $b
	$xml_obj load_main config config.xml $::main 1
}

proc write_config_dbpropeties {xml_obj} {
	set b [dict create]
	dict set b database [$xml_obj get_values]
	write_config_db daidze $b
	$xml_obj load_main config config.xml $::main 1
}

proc write_config_path {xml_obj} {
	set b [dict create]
	dict set b path [$xml_obj get_values]
	write_config_db daidze $b
	$xml_obj load_main config config.xml $::main 1
}

proc get_language_list {} {
	set language_list [list en]
	set lang [glob -nocomplain [file join lang *]]
	foreach f $lang {
		lappend language_list [file rootname [file tail $f]]
	}
	return $language_list
}

proc get_icons_list {} {
	set color_list [list]
	set color [glob -nocomplain [file join $::icons_dir *]]
	foreach f $color {
		lappend color_list [file rootname [file tail $f]]
	}
	return $color_list
}

proc top_win {top_win_name} {
	set topwin .topwin
	catch {destroy $topwin}
	toplevel $topwin
	wm title $topwin [::msgcat::mc "$top_win_name"]
	pack [set result [ttk::frame $topwin.main]] -fill x
	return $result
}

# used to execute external program like browser, calc etc.
proc open_external_program {prog args} {
	if {$args == ""} {
		exec [subst \$::$prog] &
	} else {
		foreach x $args {
			exec [subst \$::$prog] $x &
		}
	}
}

proc load_document {w} {
	$w delete 0 end
	set types {{{PDF Files} {.pdf}} {{JPEG Files} {.jpeg .jpg}} {{PNG Files} {.png}} {{All Files} *}}
	set file_to_load [tk_getOpenFile -filetypes $types]
	if {$file_to_load ne ""} {
		$w insert 0 $file_to_load
	}
}

proc set_daidze_theme {} {
	set platform [tk windowingsystem]
	set available_themes [ttk::style theme names]

	if {$platform == "x11"} {
		if {[lsearch $available_themes $::unix_default] != "-1"} {
			::ttk::style theme use $::unix_default
		}

	} elseif {$platform == "win32"} {
		if {[lsearch $available_themes $::win_default] != "-1"} {
			::ttk::style theme use $::win_default
		}

	} elseif {$platform == "aqua"} {
		if {[lsearch $available_themes $::mac_default] != "-1"} {
			::ttk::style theme use $::mac_default
		}
	}
}